package com.example.daxiguaforharmony.game.core.tween;

import com.example.daxiguaforharmony.game.core.base.DisplayObject;
import com.example.daxiguaforharmony.game.core.game.SceneBase;
import com.example.daxiguaforharmony.game.core.utils.Callback;
import com.example.daxiguaforharmony.game.core.utils.MathUtil;

public class Tween {
    private static SceneBase _scene = null;
    public static void setScene(SceneBase scene) {
        _scene = scene;
    }
    public static void moveTo(DisplayObject displayObject, float duration, float px, float py, Callback.CompleteCallback callback) {
        if (_scene == null || displayObject == null) return;
        final float[] values = {0f, 0f, displayObject.x, displayObject.y};
        final Callback.UpdateCallback updateCallback = new Callback.UpdateCallback() {
            public Object target = displayObject;
            @Override
            public void update(float dt) {
                values[0] += dt;
                values[1] = values[0] / duration;
                displayObject.x = MathUtil.lerp(values[2], px, values[1]);
                displayObject.y = MathUtil.lerp(values[3], py, values[1]);
                if (values[0] >= duration) {
                    if (callback != null) callback.complete();
                    _scene.removeUpdateCallback(this);
                }
            }
        };
        _scene.addUpdateCallback(updateCallback);
    }
    public static void scaleTo(DisplayObject displayObject, float duration, float sx, float sy, Callback.CompleteCallback callback) {
        if (_scene == null || displayObject == null) return;
        final float[] values = {0f, 0f, displayObject.scaleX, displayObject.scaleY};
        final Callback.UpdateCallback updateCallback = new Callback.UpdateCallback() {
            public Object target = displayObject;
            @Override
            public void update(float dt) {
                values[0] += dt;
                values[1] = values[0] / duration;
                displayObject.scaleX = MathUtil.lerp(values[2], sx, values[1]);
                displayObject.scaleY = MathUtil.lerp(values[3], sy, values[1]);
                if (values[0] >= duration) {
                    if (callback != null) callback.complete();
                    _scene.removeUpdateCallback(this);
                }
            }
        };
        _scene.addUpdateCallback(updateCallback);
    }
    public static void fadeTo(DisplayObject displayObject, float duration, float alpha, Callback.CompleteCallback callback) {
        if (_scene == null || displayObject == null) return;
        final float[] values = {0f, 0f, displayObject.alpha};
        final Callback.UpdateCallback updateCallback = new Callback.UpdateCallback() {
            public Object target = displayObject;
            @Override
            public void update(float dt) {
                values[0] += dt;
                values[1] = values[0] / duration;
                displayObject.alpha = MathUtil.lerp(values[2], alpha, values[1]);
                if (values[0] >= duration) {
                    if (callback != null) callback.complete();
                    _scene.removeUpdateCallback(this);
                }
            }
        };
        _scene.addUpdateCallback(updateCallback);
    }
    public static void rotateTo(DisplayObject displayObject, float duration, float rotation, Callback.CompleteCallback callback) {
        if (_scene == null || displayObject == null) return;
        final float[] values = {0f, 0f, displayObject.rotation};
        final Callback.UpdateCallback updateCallback = new Callback.UpdateCallback() {
            public Object target = displayObject;
            @Override
            public void update(float dt) {
                values[0] += dt;
                values[1] = values[0] / duration;
                displayObject.rotation = MathUtil.lerp(values[2], rotation, values[1]);
                if (values[0] >= duration) {
                    if (callback != null) callback.complete();
                    _scene.removeUpdateCallback(this);
                }
            }
        };
        _scene.addUpdateCallback(updateCallback);
    }
}
